/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import controller.LoginServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.tagext.TryCatchFinally;
import model.dao.service.UserDAOservice;
import model.entities.User;

/**
 *
 * @author Ku81
 */
public class UserDAO implements UserDAOservice {

    private static UserDAO userDAO;

    public static UserDAO getInstance() {
        if (userDAO == null) {
            userDAO = new UserDAO();

        }
        return userDAO;
    }

    //REGISTER
    @Override
    public boolean createrUser(User user) {
        boolean ischeck = false;
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "insert into tblUser values(?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            
            pstmt.setString(1, user.getUserName());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getEmail());
            pstmt.setString(4, user.getFullName());
            pstmt.setBoolean(5, user.isGender());
            pstmt.setDate(6, user.getdOB());
            pstmt.setString(7, user.getAdress());
            pstmt.setString(8, user.getTel());
            pstmt.setString(9, user.getActiveCode());
            pstmt.setBoolean(10, user.isIsActive());
            pstmt.setInt(11, user.getRolesId());
           
            pstmt.executeUpdate();
            ischeck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ischeck;

    }

    @Override
    public List<User> getUsers() {
        List<User> userList = new ArrayList<User>();

        ///
        return userList;
    }

    @Override
    public boolean checkUser(User u) {
        String sql = "SELECT * FROM tblUser WHERE username=? AND password=?";

        try {
            Connection conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, u.getUserName());
            pstmt.setString(2, u.getPassword());
            ResultSet rs = pstmt.executeQuery();
            return rs.next();


        } catch (Exception e) {
        }
        return true;

    }

      public boolean checkUserRegister(User u) {
        String sql = "SELECT * FROM tblUser WHERE username=? AND password=?";

        try {
            Connection conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, u.getUserName());
            ResultSet rs = pstmt.executeQuery();
            return rs.next();


        } catch (Exception e) {
        }
        return true;

    }
   
      
}
